package com.atguigu.tingshu.account.receiver;

import com.atguigu.tingshu.account.mapper.UserAccountDetailMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.math.BigDecimal;


@Component
public class AccountReceiver {

    @Autowired
    private UserAccountService userAccountService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_USER_REGISTER, durable = "true",autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_USER),
            key = {MqConst.ROUTING_USER_REGISTER}
    ))
    @SneakyThrows
    public void initUserAccount(
            Long userId,
            Message message,  // 接收到的消息
            Channel channel   // 当前所属的频道,用来手动确认
    ) {

        try {
            if (userId != null) {
                userAccountService.initUserAccount(userId);
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            //  deliveryTag消息的唯一标识ID ; multiple:false  表示只确认当前这一条消息。
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            // 第三个参数 requeue：true,则消息会被重新放回队列（可以再次被消费）
        }

    }


}
